﻿var allComment = [];

$(document).ready(function() {

	if ( ! document.location.href.match(/a_comment/) ){
		
		var page = [];	
		$('div.fanye a.btn_num').each(function() {
			var link = $(this).attr('href');
			if ( ! link.match(/page=1$/) ){
				page.push( link );
			}
		});

		var uid = $("#attentions").attr('href').match(/(\d+)\/follow/)[1];

		var content = [];
		var comment = [];
		$('div.MIB_feed_c').each(function() {
			var sms = $(this).find('p.sms').text();
			var mid = $(this).find('p.sms').attr('mid');
			$(this).find('p.source span').remove();
			var source = $(this).find('p.source').text();
			sms = trim(sms);
			source = trim(source);
			var date = $(this).find('div.lf strong').attr('date');
			content.push( mid + '||blog||' + sms + '%%' + source + '||' + date);
			
			var fid = $(this).html().match(/fid=([a-zA-Z0-9]+)/)[1];
			var cmpage = [];
			cmpage.push(mid);
			cmpage.push( 'http://weibo.com/'+ uid + '/' + fid + '#a_comment' );
			
			comment.push( cmpage );
		});

		chrome.extension.sendRequest( {'action': 'data' , 'page': page,  'comment': comment, 'content': content });
	}
});

$(document).ready(function() {
	
	window.setTimeout(check, 3000);

});

function check() {
	
	$('p.commentsParm').each(function(){
			allComment.push( trim( $(this).text() ) );
	});

	if ( $('div.fanye a.btn_normal:last').text().match(/下一页/) ) {
	
		var triggerb = document.createEvent('Event');
		triggerb.initEvent('click', true, true);
		$('div.fanye a.btn_normal:last')[0].dispatchEvent(triggerb);
		
		window.setTimeout(check, 2000);
		
	} else {

		chrome.extension.sendRequest( {'action': 'comment' ,'comment': allComment });
	
	}
}


function  trim(str){
	str = str.replace(/\n+/, '');
	str = str.replace(/^\s+/, '');
	str = str.replace(/\s+$/, '');
	str = str.replace(/ +$/, '');
	return str;
}